package com.emip.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.SecondStoreBean;
import com.emip.dao.SecondStoreDAO;
import com.emip.util.ReportFormat;



@Repository("secondStoreDAO")
public class SecondStoreDAOImpl extends StoreDAO implements SecondStoreDAO {

	final String SQLDATEFORMAT = "YYYY-MM-DD HH24:MI:SS";
	
	public List<SecondStoreBean> getByTimespan(String startDate, String endDate) {
		return this.getByTimespan(startDate, endDate, null, null);
	}
	
	@SuppressWarnings("unchecked")
	public List<SecondStoreBean> getByTimespan(String startDate, String endDate, String[] items, String siteId) {
		String hql = this.getSecondHQL(startDate, endDate, items, siteId);
		hql += ", entity.item.id";
		return this.getHibernateTemplate().find(hql);
	}

	public SecondStoreBean merge(SecondStoreBean store) {
		return getHibernateTemplate().merge(store);
	}

	public long getCount(String startDate, String endDate, String[] items, String siteId) {
		String hql = "select count(distinct entity.genTime) " + this.getSecondHQL(startDate, endDate, items, siteId);
		return (Long)getCount(hql);
	}
	
	public long getCount(String date, String[] items, String siteId) {
		String hql = "select count(distinct entity.genTime) " + this.getSecondHQL(date + " 00:00:00", date + " 23:59:59", items, siteId);
		return (Long)getCount(hql);
	}

	public SecondStoreBean findById(int id) {
		return getHibernateTemplate().get(SecondStoreBean.class, id);
	}

	public List<SecondStoreBean> getByTimespan(String startDate,
			String endDate, String[] items, String siteId, int page, int size) {
		return getDatePage(SecondStoreBean.class, ReportFormat.SECONDFORMAT, startDate, endDate, items, siteId, page, size);
	}

	@SuppressWarnings("unchecked")
	public Date getLastDate(String itemId, String siteId) {
		String hql = "select max(b.genTime) from SecondStoreBean b where 1 = 1";
		if (itemId != null) {
			hql += " and b.item.id = '" + itemId + "'";
		}
		if (siteId != null) {
			hql += " and b.site.id = '" + siteId + "'";
		}
		List<Date> list = getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public List<SecondStoreBean> getLastNItemsBySiteID(String siteId,
			String itemId, int num) {
		String hql = "from SecondStoreBean s where s.site.id = '" + siteId +
				"' and s.item.id = '" + itemId + "' order by s.genTime desc";
		return getEntities(SecondStoreBean.class, hql, 1, num);
	}
	
	public Object[] getStatistics(String startDate, String endDate, String itemIds, String siteId) {
		long count = this.getCount(startDate, endDate, new String[]{itemIds}, siteId);
		if (count == 0) return null;
		String[] result = this.getDateStatistic("Second", "YYYY-MM-DD HH24:Mi:SS", startDate, endDate, new String[]{itemIds}, siteId, "YYYY-MM-DD HH24");
		result[3] = Integer.parseInt(result[3])*100/count + "%";
		return result;
	}

	public List<Object> getNormalStat(String startDate, String endDate, String itemIds, String siteId) {
		return this.getNormalDateStat("Second", "YYYY-MM-DD HH24:Mi:SS", startDate, endDate, new String[]{itemIds}, siteId, "YYYY-MM-DD HH24");
	}

	public List<Object> getStat(String startDate, String endDate, String itemIds, String siteId) {
		return this.getDateStat("Second", "YYYY-MM-DD HH24:Mi:SS", startDate, endDate, new String[]{itemIds}, siteId, "YYYY-MM-DD HH24");
	}

	public SecondStoreBean findBySecond(String date, String siteId,
			String itemId) {
		List<SecondStoreBean> seconds = this.getByTimespan(date, date, new String[]{itemId}, siteId);
		if (seconds != null && seconds.size() > 0) {
			return seconds.get(0);
		}
		return null;
	}
}
